


local dir 
do "./eventstudy/auxcodes.do"

foreach sample in  "" {
foreach event in headsev headsevanysp headanysp headmod  headsevspwork headsevnwork headsevnnowork {
*headsev head headsev_di headsev_np headmod headsev_di headsev_nodi  

set scheme s1mono




*headsev_nodi
*headmod head spouse both 
use "./Full_Panel_2b.dta", clear

replace married = coupled

replace spending = . if spending_miss >0 & spending_miss != .

if("`sample'" == "highed"){
 keep if sample_select == 1
 keep if educ > 2 & educ != .
 }
else keep if sample_health==1

rename sample_leavenext leavenext
rename sample_dienext dienext

*If DI is missing, assume no DI receipt, relevant for like 6 obs.
replace DI =0 if DI ==.

gen empnowDI = empnow==1 & DI==1

*use "`dir'/lpdata_withspouse.dta", clear
*replace birthy=round(birthy,5)
gen n=1
gen haspartner = agew > 0 & agew <999
bysort newid: egen panellength=total(n)
gen adults = fsize-kids
drop n
gen trhw_nonDI = trhw if DI!=1
gen hsgrad=educ==2
gen marnow=married

gen w_no_bus_res = w_no_bus - home_equity - cash - stocks - vehicles -ira

gen hours_work = hours if hours > 0 & hours !=.

gen wage = ly/hours if ly > 0 & hours > 0
gen sp_wage = wly/sp_hours if wly > 0 & sp_hours > 0 & sp_hours != .
*killing sp_wage among non-married for convenience:
replace sp_wage =0 if married==0 

gen lyWork = ly if ly >0
gen wlyWork = wly if wly >0



gen hasdis = DS >=1 & DS !=.
gen sp_hasdis = sp_DS >=1 & sp_DS !=.

bysort newid: egen maxyear = max(year)
gen lastobs = year==maxyear
*Only counting your death or leaving household if you don't later come back
replace leavenext = leavenext ==1 & lastobs==1
replace dienext = dienext ==1 & lastobs==1
gen dropnext = dienext==0 & leavenext==0 & (maxyear!= 2016 & age != 62) & lastobs==1
gen endsample = dienext==0 & leavenext==0 & (maxyear == 2016 | age == 62)& lastobs==1


gen rent_f = rent
gen totfood_f=totfood
gen spend_imp = exp(lc_impute)
ren lc_impute lc_imp


****DEFINING EVENT TYPE FOR EVENT STUDIES:
*FIX AGE IN ALL THIS.

global savethreshold 10

gen disgroup =.
replace disgroup = 0 if sev==0 & mod == 0
replace disgroup = 1 if sev== 0 & mod == 1
replace disgroup = 2 if sev==1

gen lc=ln(spending)

replace ly = ly + nlyhw

gen 	scale=1+0.5*(fsize-kids-1)+0.3*kids		
/*OECD modified scale*/

replace totfood = totfood + food_out

foreach x of varlist wly ly nlyhw y trhw troth tyoth nonhy tr_* soc_sec uihead uiwife wcomp wcompw alltax taxsim y_posttax lss_posttax totfood_f rent_f  trall_nonss spend_imp totfood health education util childcare transport rent homeinsure home_equity bus_ass real_estate stocks vehicles other_ass ira other_debt {
gen `x'_eq = `x'/scale
}

replace lc = . if year <= 1996
replace spending = . if year <= 1996



gen spending_eq = spending/(1+0.5*(fsize-kids-1)+0.3*kids)
gen w_with_h_eq = w_with_h/scale
gen w_no_bus_eq = w_no_bus/scale
gen w_no_h_eq = w_no_h/scale


gen logspending = ln(spending)
gen logspending_eq = ln(spending_eq)
gen logw_with_h = ln(w_with_h)
gen logw_no_h = ln(w_no_h)
gen logw_no_bus= ln(w_no_bus)
gen logw_with_h_eq = ln(w_with_h_eq)
gen logw_no_h_eq = ln(w_no_h_eq)
gen logw_no_bus_eq = ln(w_no_bus_eq)


foreach x of varlist wly_eq ly_eq nlyhw_eq y_eq tyoth_eq nonhy_eq trhw_eq troth_eq trhw_nonDI totfood_f_eq rent_f_eq  spend_imp_eq totfood health education util childcare transport rent homeinsure totfood_eq health_eq education_eq util_eq childcare_eq transport_eq rent_eq homeinsure_eq home_equity bus_ass stocks vehicles other_ass ira spending spending_eq w_with_h* w_no_h* w_no_bus* tr_*eq soc_sec_eq uihead_eq uiwife_eq wcomp_eq wcompw_eq taxsim alltax y_posttax lss_posttax taxsim_eq alltax_eq y_posttax_eq lss_posttax_eq trall_nonss_eq {
replace `x' = `x'/1000
}

					
gen lc_imp_eq = lc_imp -ln(scale)
gen logy_eq = ln(y_eq)
gen logy_posttax_eq = ln(y_posttax_eq)
gen logtaxsim_eq = ln(taxsim_eq)
gen logly_eq  = ln(ly_eq)
gen logwly_eq = ln(wly_eq)
gen logrent_f_eq = ln(rent_f_eq)
gen logtotfood_f_eq = ln(totfood_f)

replace lc=lc-ln(scale)


gen mod_DI=mod*DI
gen sev_DI=sev*DI

gen nodis = sev==0&mod==0

*gen SSIrec = tr_ssi_eq>1 & tr_ssi_eq!=.
*gen anyDI = SSIrec==1 | DI == 1
gen emp = hours >=1500 & hours!=. 
gen emp_part = hours>0&hours<1500
gen nowork_noDI = emp !=1 & emp_part != 1 & DI!=1 

gen empspouse = sp_hours>=1500 & sp_hours!= .
gen empspouse_part = sp_hours>0&sp_hours<1500
*merge m:1 newid using "`dir'/Low and Pistaferri/Code LP AER_Empirical/sp_temp_type"
*drop _merge
*make types unique by head--need
*merge m:1 sp_newid using "`dir'/Low and Pistaferri/Code LP AER_Empirical/sp_temp_type_spouse"

bysort married: sum BMI obese underw trouble* arthritis asthma bloodp cancer diabetes emotion heart_att heart_dis lung_dis stroke  if DS == 2
bysort married: sum BMI obese underw trouble* arthritis asthma bloodp cancer diabetes emotion heart_att heart_dis lung_dis stroke  if anyDI==1
bysort anyDI married: sum BMI obese underw trouble* arthritis asthma bloodp cancer diabetes emotion heart_att heart_dis lung_dis stroke  if DS ==2

*************SAMPLE STATISTICS****:
bysort year married: egen npeople=sum(pid)
bysort newid married: egen nwaves=sum(pid)
gen weighttemp = 1/npeople
gen weighttemp2 = 1/nwaves

local agelab "Age"
local birthylab "Birth Year"
local hsgradlab "HS Grad"
local whitelab "White"
local adultslab "Number of Adults"
local kidslab "Number of Kids"
local modlab "Mod. Dis."
local sevlab "Sev. Dis."
local haspartnerlab "Has Partner"
local w_with_hlab "Household Net Worth"
local workyearslab "Years Worked"
local workyears_fulllab "Years Worked Full-Time"
local head_eliglab "Eligible for DI"
local npeoplelab "\# Households/Wave"
local nwaveslab "\# Waves/Household"
local trouble_walklab "Walking or Climbing Stairs"
local trouble_bendlab "Bending or Lifting"
local trouble_drivelab "Driving"
local trouble_eyelab "Seeing"
local trouble_mealslab "Preparing Own Meals (2003-2017)"
local trouble_shoplab "Personal Shopping (2003-2017)"
local trouble_moneylab "Managing Money (2003-2017)"
local trouble_phonelab "Using the Telephone (2003-2017)"
local trouble_heavyworklab "Doing Heavy Housework (2003-2017)"
local trouble_lightworklab "Doing Light Housework (2003-2017)"
local arthritislab "Arthritis (1999-2017)"
local asthmalab "Asthma  (1999-2017)"
local bloodplab "Hypertension (1999-2017)"
local cancerlab "Cancer (1999-2017)"
local diabeteslab "Diabetes (1999-2017)"
local emotionlab "Psychiatric Problems  (1999-2017)"
local heart_attlab "Heart Attack   (1999-2017)"
local heart_dislab "Heart Disease  (1999-2017)"
local lung_dislab "Lung Disease  (1999-2017)"
local strokelab "Stroke (1999-2017)"

local statvars age birthy hsgrad white adults kids mod sev haspartner w_with_h workyears workyears_full head_elig npeople nwaves
local healthvars trouble_walk trouble_bend trouble_drive trouble_eye trouble_meals trouble_shop trouble_money trouble_phone trouble_heavywork trouble_lightwork
local diagvars arthritis asthma bloodp cancer diabetes emotion heart_att heart_dis lung_dis stroke

egen trouble = rowmax(trouble_walk trouble_bend trouble_drive trouble_eye trouble_meals trouble_shop trouble_money trouble_phone trouble_heavywork trouble_lightwork)
egen diagnostic = rowmax(cancer heart_att heart_dis lung_dis stroke)


bysort newid: egen firstyear = min(year)
bysort newid: egen firstage = min(age)

*Get rid of people who are never observed as healthy
*Issue with these guys is I don't know age of onset.

if "`event'"=="enterDI"{
*keep if disgroup>0
bysort newid: egen everanydis=max(DI)
bysort newid: egen anyonset_age = min(age+ 99999*((DI)==0))
replace anyonset_age = . if anyonset_age >=99999
*replace anyonset_age = . if firstage==anyonset_age 
drop firstyear

bysort newid: egen everdis=max(DI)
bysort newid: egen onset_age = min(age+ 99999*(DI==0))
}
else{
bysort newid: egen everanydis=max(disgroup+sp_DS)
bysort newid: egen anyonset_age = min(age+ 99999*((disgroup+sp_DS)==0))
bysort newid: egen anyonset_ageh = min(age+ 99999*((disgroup)==0))
replace anyonset_age = . if anyonset_age >=99999
replace anyonset_ageh = . if anyonset_ageh >=99999
*replace anyonset_age = . if firstage==anyonset_age 
drop firstyear

*if not doing an event study of DI receipt
*then doing one of disability onset, where 
*control group is healthy people
*Now, take each disabled person and match them to comparable people
bysort newid: egen onset_age_hsev = min(age+ 99999*(disgroup!=2)+99999*(sp_DS!=0))
bysort newid: egen onset_age_hsev_sp = min(age+ 99999*(disgroup!=2))

*Now, take each disabled person and match them to comparable people
bysort newid: egen onset_age_hsevdi = min(age+ 99999*(disgroup!=2)+99999*(sp_DS!=0))
bysort newid: egen getsDI = max(DI==1&age>=onset_age_hsevdi & age<=onset_age_hsevdi+6)
replace onset_age_hsevdi =. if getsDI==0


*Now, take each disabled person and match them to comparable people
bysort newid: egen onset_age_hsevnodi = min(age+ 99999*(disgroup!=2)+99999*(sp_DS!=0))
replace onset_age_hsevnodi =. if getsDI==1

*Now, take each disabled person and match them to comparable people
bysort newid: egen onset_age_hmod = min(age+ 99999*(disgroup!=1)+99999*(sp_DS!=0))
bysort newid: egen onset_age_hmod_sp = min(age+ 99999*(disgroup!=1))

*Now, take each disabled person and match them to comparable people
bysort newid: egen onset_age_h = min(age+ 99999*(disgroup==0)+99999*(sp_DS!=0))

*Now, take each disabled person and match them to comparable people
bysort newid: egen onset_age_hb = min(age+ 99999*(disgroup==0))

*Now, take each disabled person and match them to comparable people
bysort newid: egen onset_age_sp = min(age+ 99999*(disgroup!=0)+99999*(sp_DS==0))
bysort newid: egen onset_age_spmod = min(age+ 99999*(disgroup!=0)+99999*(sp_DS!=1))
bysort newid: egen onset_age_spsev = min(age+ 99999*(disgroup!=0)+99999*(sp_DS!=2))

bysort newid: egen onset_age_spmod_h = min(age + 99999*(sp_DS!=1))
bysort newid: egen onset_age_spsev_h = min(age + 99999*(sp_DS!=2))

*Now, take each disabled person and match them to comparable people
bysort newid: egen onset_age_b = min(age+ 99999*(disgroup*sp_DS==0))

gen eventtype = .
gen eventtype_sp = .
*treatment is first transition from healthy to disabled, for head
*spouse may be disabled at the same time, but not BEFORE head
replace eventtype = 1 if onset_age_hmod_sp == anyonset_age
replace eventtype = 2 if onset_age_hsev_sp == anyonset_age
replace eventtype = 0 if eventtype == . & anyonset_age != .

replace eventtype_sp = 1 if onset_age_spmod_h == anyonset_age
replace eventtype_sp = 2 if onset_age_spsev_h == anyonset_age
replace eventtype_sp = 0 if eventtype_sp ==. & anyonset_age != .

egen onset_age = rowmin(onset_age*)
replace onset_age = . if firstage==onset_age

if "`event'"=="both"{ 		
replace onset_age = . if onset_age >= 99999
}
if "`event'"=="spouse"{ 	
replace onset_age = . if eventtype_sp == 0
}
if "`event'"=="headorboth"{ 	
replace onset_age = . if eventtype == 0	
}	
if "`event'"=="head"{ 		
replace onset_age = . if eventtype == 0 | eventtype_sp != 0
}
if "`event'"=="headmod"{ 		
replace onset_age = . if eventtype != 1 | eventtype_sp != 0
}
if("`event'"=="headsevanysp"){
replace onset_age = onset_age_hsev_sp 
replace onset_age = . if onset_age_hsev_sp>anyonset_ageh | onset_age_hsev_sp == .
replace anyonset_age = anyonset_ageh
}
if("`event'"=="headanysp"){
replace onset_age = onset_age_hb 
replace onset_age = . if onset_age_hb>anyonset_ageh | onset_age_hb == .
replace anyonset_age = anyonset_ageh
}
if "`event'"=="headsev" | "`event'"=="headsev_np" | "`event'" == "headsevspwork"{
replace onset_age = . if eventtype != 2 | eventtype_sp != 0
}
if "`event'"=="headsev_nodi"{
replace onset_age = . if eventtype != 2 | getsDI == 1| eventtype_sp != 0 
}
if "`event'"=="headsev_di"{
replace onset_age = . if eventtype != 2 | getsDI == 0 | eventtype_sp != 0 
}

gen time = 100+age-onset_age

}
**Killing treated guys where disabilities are not exactly coincident:
*replace onset_age = . if anyonset_age < onset_age 
replace onset_age = . if onset_age >=99999

*Issue with these guys below is I don't know age of onset.
bysort newid: egen time99=max(time==99)
bysort newid: egen time98=max(time==98)
bysort newid: egen onset_year = max(year*(onset_age==age))
replace onset_age = . if time99==0 & onset_year <=1996
replace onset_age = . if time98==0 & onset_year >1996

drop onset_year time99 time98 time


bysort newid: egen obs = count(1)

drop married

gen yeardiff = 1 if year < 1996
replace yeardiff = 2 if year >= 1996

sort newid year
by newid: gen empyet=sum(emp)
replace empyet = empyet >= 1
by newid: gen anyempspouseyet=sum(sp_hours > 0 & sp_hours != .)
replace anyempspouseyet = anyempspouseyet >= 1
 
 
 
 save "./eventstudy/psid_cleaned_`event'`sample'", replace
 


}
}
